<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <el-row :gutter="20" class="orderMesTop">
        <el-col :span="12">
          <div class="orderMesDiv">
            <span class="orderMesTop-span">订单信息:</span>
            <el-button v-if="list.status == 0" type="warning">待处理</el-button>
            <el-button v-if="list.status == 1" type="danger">退货中</el-button>
            <el-button v-if="list.status == 2" type="warning"
              >已完成</el-button
            >
            <el-button v-if="list.status == 3" type="info">已拒绝</el-button>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="20" class="orderMes">
        <el-col :span="6">
          订单: {{ list.orderSn }} <span>查看订单详情</span></el-col
        >
        <el-col :span="6"> 退单人姓名: {{ list.returnName }}</el-col>
        <el-col :span="6"> 退单理由: {{ list.reason }}</el-col>
        <el-col :span="6"> 退单描述: {{ list.description }}</el-col>
      </el-row>
      <el-row :gutter="20" class="mesBottom">
        <el-col :span="6"> 申请时间:: {{ list.createTime }}</el-col>
      </el-row>
      <div v-if="list.status == 2 || list.status == 3">
        <el-row :gutter="20" class="mesBottom">
          <el-col :span="6">
            <div class="statusSpan">退单图片:</div>
            <div class="statusImg">
              <img
                v-for="item in list.proofPics"
                :key="item"
                class="img"
                :src="item"
                alt=""
              />
            </div>
          </el-col>
        </el-row>
      </div>
      <el-row :gutter="20" class="orderMesTop">
        <el-col :span="12" :offset="0">
          <div class="orderMesDiv">
            <span class="orderMesTop-span">商品信息:</span>
          </div>
        </el-col>
      </el-row>

      <el-table :data="tableShowData" border style="width: 100%">
        <el-table-column label="商品图片" align="center">
          <template slot-scope="scope">
            <img class="img" :src="scope.row.productPic" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="商品描述" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.productName }}</div>
            <div>{{ scope.row.productBrand }}</div>
          </template>
        </el-table-column>

        <el-table-column
          prop="productRealPrice"
          label="商品价格"
          align="center"
        >
        </el-table-column>

        <el-table-column label="商品属性" width="180" align="center">
          <template slot-scope="scope">
            <div v-for="item in scope.row.productAttr" :key="item.key">
              <span>{{ item.key + ":" + item.value }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="productCount" label="商品数量" align="center">
        </el-table-column>

        <el-table-column prop="returnAmount" label="总计" align="center">
        </el-table-column>
      </el-table>

      <el-row :gutter="20" class="orderMesTop">
        <el-col :span="12" :offset="0">
          <div class="orderMesDiv">
            <span class="orderMesTop-span">用户信息:</span>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="20" class="orderUserMes">
        <el-col :span="6"> 退单人姓名: {{ list.returnName }}</el-col>
        <el-col :span="6"> 退单理由: {{ list.returnPhone }}</el-col>
      </el-row>

      <el-row :gutter="20" class="orderMesTop">
        <el-col :span="12" :offset="0">
          <div class="orderMesDiv">
            <span class="orderMesTop-span">费用信息:</span>
          </div>
        </el-col>
      </el-row>

      <div v-if="list.status == 0">
        <el-row :gutter="20" class="orderUserMes left">
          <el-col :span="8"> 退单金额: {{ list.returnAmount }}</el-col>
          <el-col :span="8"> 运费金额: {{ list.productRealPrice }}</el-col>
        </el-row>
        <el-form
          :rules="rules"
          ref="formData"
          :model="formData"
          label-width="80px"
        >
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="退款金额">
                <el-input-number
                  v-model="formData.returnAmount"
                  :min="-100"
                  :max="10"
                ></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="处理人" prop="handleMan">
                <el-input v-model="formData.handleMan"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="处理备注" prop="handleNote">
                <el-input v-model="formData.handleNote"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <el-row :gutter="20">
          <el-col :span="12" :offset="10">
            <el-button type="primary" size="default" @click="agreen"
              >同意退款</el-button
            >
            <el-button type="info" size="default">拒绝退款</el-button>
          </el-col>
        </el-row>
      </div>

      <div v-if="list.status == 1">
        <el-row :gutter="20" class="orderUserMes left">
          <el-col :span="8"> 退单金额: {{ list.returnAmount }}</el-col>
          <el-col :span="8"> 运费金额: {{ list.productRealPrice }}</el-col>
          <el-col :span="8"> 处理时间: {{ list.handleTime }}</el-col>
        </el-row>
        <el-form
          :rules="rules"
          ref="formData"
          :model="formData"
          label-width="80px"
        >
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="收货人" prop="name">
                <el-input v-model="formData.receiveMan"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="收货备注" prop="title">
                <el-input v-model="formData.receiveNote"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <el-row :gutter="20">
          <el-col :span="12" :offset="10">
            <el-button type="primary" size="default" @click="agreen"
              >确认收货</el-button
            >
          </el-col>
        </el-row>
      </div>

      <div v-if="list.status == 2">
        <el-row :gutter="20" class="orderUserMes left">
          <el-col :span="8"> 退单金额: {{ list.returnAmount }}</el-col>
          <el-col :span="8"> 运费金额: {{ list.productRealPrice }}</el-col>
        </el-row>

        <el-row :gutter="20" class="orderMesTop">
          <el-col :span="12" :offset="0">
            <div class="orderMesDiv">
              <span class="orderMesTop-span">处理结果:</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="orderUserMes left">
          <el-col :span="8"> 处理人: {{ list.handleMan }}</el-col>
          <el-col :span="8"> 处理备注: {{ list.handleNote }}</el-col>
          <el-col :span="8"> 处理时间: {{ list.handleTime }}</el-col>
        </el-row>

        <el-row :gutter="20" class="orderMesTop">
          <el-col :span="12" :offset="0">
            <div class="orderMesDiv">
              <span class="orderMesTop-span">收货信息:</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="orderUserMes left">
          <el-col :span="8"> 退单收货人: {{ list.receiveMan }}</el-col>
          <el-col :span="8"> 退单收货备注: {{ list.receiveNote }}</el-col>
          <el-col :span="8"> 退单收货时间: {{ list.receiveTime }}</el-col>
        </el-row>
      </div>

      <div v-if="list.status == 3">
        <el-row :gutter="20" class="orderUserMes left">
          <el-col :span="8"> 退单金额: {{ list.returnAmount }}</el-col>
          <el-col :span="8"> 运费金额: {{ list.productRealPrice }}</el-col>
        </el-row>

        <el-row :gutter="20" class="orderMesTop">
          <el-col :span="12" :offset="0">
            <div class="orderMesDiv">
              <span class="orderMesTop-span">处理结果:</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="orderUserMes left">
          <el-col :span="8"> 处理人: {{ list.handleMan }}</el-col>
          <el-col :span="8"> 处理备注: {{ list.handleNote }}</el-col>
          <el-col :span="8"> 处理时间: {{ list.handleTime }}</el-col>
        </el-row>
      </div>
    </el-card>

    <buttomBanner></buttomBanner>
  </div>
</template>

<script>
import buttomBanner from "@/components/buttomBanner";
import {
  orderReturn,
  agreeApply,
  receiveProduct
} from "@/api/order/backOrder/backOrderDetail/backOrderDetail.js";
export default {
  data() {
    return {
      list: {},
      api:agreeApply,
      formData: {
        returnAmount: 0,
        handleMan: "",
        handleNote: "",
      },
      tableShowData: [],
      rules: {
        handleMan: [
          { required: true, message: "请输入处理人名字", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
        handleNote: [
          { required: true, message: "请输入处理备注", trigger: "blur" },
          { trigger: "blur" },
        ],
      },
    };
  },
  components: {
    buttomBanner,
  },
  methods: {
    //   同意退款
    agreen() {
      var obj = this.formData;
      obj.id = this.list.id;
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.api(this.list.id, obj).then((res) => {
            if (res.success) {
              this.$message.success("提交成功");
              this.tableShowData = [];
              this.init(this.list.id);
              this.formData = {
                receiveMan: "",
                receiveNote: "",
              };
              this.api = receiveProduct
            } else {
              this.$message.error(res.message);
            }
          });
        }
      });
    },
    init(id) {
      orderReturn(id).then((res) => {
        console.log(res);
        if (res.success) {
          var el = res.data.orderReturnApply;
          if (Array.isArray(JSON.parse(el.productAttr))) {
            el.productAttr = JSON.parse(el.productAttr);
          } else if (Array.isArray(JSON.parse(JSON.parse(el.productAttr)))) {
            el.productAttr = JSON.parse(JSON.parse(el.productAttr));
          } else if (
            Array.isArray(JSON.parse(JSON.parse(JSON.parse(el.productAttr))))
          ) {
            el.productAttr = JSON.parse(JSON.parse(JSON.parse(el.productAttr)));
          }

          el.proofPics = el.proofPics.split(",");
          this.list = el;
          this.tableShowData.push(el);
          console.log(this.list, this.tableShowData);
        } else {
          this.$message.error(res.message);
        }
      });
    },
  },
  created() {
    var item = JSON.parse(window.localStorage.getItem("backOrderData"));
    console.log(item.id);
    this.init(item.id);
  },
};
</script>

<style lang="scss" scoped>
.orderMesDiv {
  padding: 40px 0 15px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .orderMesTop-span {
    font-weight: 700;
    font-size: 25px;
    margin-right: 10px;
  }
}
.orderMes {
  padding: 20px 0;
}
.mesBottom {
  padding: 0 0 40px;
  border-bottom: 1px solid #e3e3e3;
}
.img {
  width: 100px;
  height: 100px;
}
.orderUserMes {
  padding: 0;
}
.orderUserMes.left {
  margin-bottom: 10px;
}
.el-input-number {
  width: auto;
}
.statusImg {
  display: flex;
}
.statusSpan {
  font-weight: 700;
  font-size: 25px;
  margin: 10px 0;
}
</style>